package com.haha.helloworld.recyclerview;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.haha.helloworld.R;

public class PuRecyclerViewActivity extends AppCompatActivity {
    private RecyclerView rv_pu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pu_recycler_view);
        rv_pu = (RecyclerView) findViewById(R.id.rv_pu);
        //两个参数，表示2行 且 垂直
        rv_pu.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        rv_pu.addItemDecoration(new MyDecoration());
        rv_pu.setAdapter(new StaggeredGridAdapter(PuRecyclerViewActivity.this, new StaggeredGridAdapter.OnItemClickListener() {
            @Override
            public void onClick(int pos) {
                Toast.makeText(PuRecyclerViewActivity.this, "点击了第" + pos + "个item", Toast.LENGTH_SHORT).show();
            }
        }));

    }
    //自定义样式
    class MyDecoration extends RecyclerView.ItemDecoration {
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            //读取dimens文件数据，用于设置样式
            int gap = getResources().getDimensionPixelSize(R.dimen.dividerHeight_more);
            //渲染上下左右都是gap
            outRect.set(gap, gap, gap, gap);
        }
    }
}